<?php
namespace app\api\model\appointment;

use app\common\model\BaseModel;

class Service extends BaseModel
{
    protected $name = 'appointment_service';

    public function lists($user_id)
    {
        $where = [];
        if ($user_id > 0) $where['s.service_id'] = $user_id;
        //$where['s.pay_status'] = 0;

        $pay_status = [0 => '未结算', 1 => '已结算'];
        return $this->alias('s')
            ->join('appointment_order order', 'order.id = s.order_id', 'left')
            ->join('appointment_type type', 'type.type_id = order.type_id', 'left')
            ->join('appointment_time time', 'time.time_id = order.time_id', 'left')
            ->join('server_user user', 'user.user_id = s.service_id', 'left')
            ->where([
                's.status' => 1,
                's.is_delete' => 0,
                's.wxapp_id' => self::$wxapp_id,
            ])->where($where)->field('order.num onum,order.service_id,order.type_id,order.user_goods_id,order.order_id order_no,type.name type_name,order.appointment_date date,time.desc time_name,
             order.user_name,order.user_phone,order.address,user.real_name server_name,user.mobile server_phone,
             s.start_time,s.end_time,s.use_time,s.price,s.service_price,order.offline_name,s.pay_status,order.id order_id,s.real_price,s.reward_price,s.remark')
            ->order(['s.pay_time'=> 'desc','s.create_time'=>'desc'])->
            paginate(1000, false, [
                'query' => \request()->request()
            ])->each(function ($item, $key) use ($pay_status) {
                $item['date'] = date("Y-m-d", $item['date']);
                //if ($item['use_time'] == 0) {
                    $hastime = $this->timediff($item['start_time'],$item['end_time']);
                    $item['use_time'] = $hastime['hour']."时".$hastime['min']."分";
                    /*$timediff = intval($item['end_time']) - intval($item['start_time']);
                    $item['use_time'] = number_format($timediff / 60, 0, '.', '');*/
                //}
                if(!$item['price']){
                    $item['price'] = 0;
                }
                //$jiang = $item['real_price']-$item['service_price'];
                //$item['jiang'] = $jiang;
                $item['start_time'] = $item['start_time'] > 0 ? date("Y-m-d H:i", $item['start_time']) : '/';
                $item['end_time'] = $item['end_time'] > 0 ? date("Y-m-d H:i", $item['end_time']) : '/';
                $item['pay_status_name'] = $pay_status[$item['pay_status']];
                $item['service_price'] = $item['service_price'] * $item['onum'];
            });
    }
    function timediff($begin_time,$end_time)
    {
        if ( $begin_time < $end_time ) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        $timediff = $endtime - $starttime;
        $days = intval( $timediff / 86400 );
        $remain = $timediff % 86400;
        $hours = intval( $remain / 3600 );
        $remain = $remain % 3600;
        $mins = intval( $remain / 60 );
        $secs = $remain % 60;
        $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs );
        return $res;
    }

}